package com.shujia.flinkcore;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.connector.file.src.FileSource;
import org.apache.flink.connector.file.src.reader.TextLineInputFormat;
import org.apache.flink.connector.jdbc.JdbcConnectionOptions;
import org.apache.flink.connector.jdbc.JdbcSink;
import org.apache.flink.core.fs.Path;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;

import java.time.Duration;

public class CarsToMysql {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //开启checkpoint
        env.enableCheckpointing(5000);

        //1、读取数据
        FileSource<String> carSource = FileSource
                .forRecordStreamFormat(new TextLineInputFormat(), new Path("flink/data/cars"))
                .monitorContinuously(Duration.ofSeconds(5))
                .build();

        DataStream<String> carsDS = env
                .fromSource(carSource, WatermarkStrategy.noWatermarks(), "car");

        //2、解析数据
        DataStream<Tuple2<String, Integer>> kvDS = carsDS
                .map(car -> {
                    String county = car.split(",")[2];
                    return Tuple2.of(county, 1);
                }, Types.TUPLE(Types.STRING, Types.INT));

        //3、实时拥挤区县的车流量
        DataStream<Tuple2<String, Integer>> countyFlowDS = kvDS
                .keyBy(kv -> kv.f0)
                .sum(1);

        //4、将计算结果保存到数据库中
        //replace into: 如果不存在就插入，如果存在就替换
        SinkFunction<Tuple2<String, Integer>> mysqlSink = JdbcSink.sink(
                "replace into county_flow values (?,?)",
                (ps, kv) -> {
                    String county = kv.f0;
                    Integer flow = kv.f1;
                    ps.setString(1, county);
                    ps.setInt(2, flow);
                },
                new JdbcConnectionOptions.JdbcConnectionOptionsBuilder()
                        .withUrl("jdbc:mysql://192.168.205.129:3306/bigdata27?useUnicode=true&characterEncoding=UTF-8&useSSL=false")
                        .withDriverName("com.mysql.cj.jdbc.Driver")
                        .withUsername("root")
                        .withPassword("123456")
                        .build());

        //使用mysql sink
        countyFlowDS.addSink(mysqlSink);

        env.execute();
    }
}
